//
//  D_BgImageViewController.m
//  音悦咖
//
//  Created by dllo on 15/5/1.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "D_BgImageViewController.h"

@interface D_BgImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *imgArr;

@end

@implementation D_BgImageViewController

- (void)dealloc
{
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 30, SCREEN_WIDTH / 2 - 20);
    layout.sectionInset = UIEdgeInsetsMake(30, -60, 100, 90);
    layout.minimumLineSpacing = 20;
     
    
    UICollectionView *collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    collect.delegate = self;
    collect.dataSource = self;
    [self.view addSubview:collect];
    collect.backgroundColor = [UIColor clearColor];
    [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    self.imgArr = [NSMutableArray array];
    // 图片数组
    self.imgArr = [@[@"img1.jpg",@"img2.jpg",@"img3.jpg",@"img4.jpg",@"img5.jpg",@"img6.jpg",@"img7.jpg",@"img8.jpg",@"img9.jpg",@"img10.jpg",@"img11.jpg",@"img12.jpg",@"img13.jpg",@"img14.jpg",@"img15.jpg",@"img16.jpg",@"bg.jpg",@"img18.jpg",@"img19.jpg",@"img20.jpg"] mutableCopy];

    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
//    [cell.layer setCornerRadius:10];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
    imgView.image = [UIImage imageNamed:self.imgArr[indexPath.item]];
    imgView.userInteractionEnabled = YES;
    [cell addSubview:imgView];
    return cell;
    
   
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgArr.count;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
////    //通知中心
//    NSString *str = self.imgArr[indexPath.item];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"imgcom" object:str userInfo:@{@"name":str}];
//
    // block
    self.block(indexPath.item);
    
     [self.navigationController popToRootViewControllerAnimated:YES];
}

// 动画
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D rotation;
    // 更换角度
    rotation = CATransform3DMakeRotation((90.0 * M_PI)/ 180, 0.0, 0.7, 0.4);
    rotation.m34 = 1.0/ - 600;
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    cell.layer.transform = rotation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);
    // rotation
    [UIView beginAnimations:@"push" context:nil];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
